﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using SPAdvancedSearch.Inputs;
using System.Web;
using System.Text.RegularExpressions;
using SPAdvancedSearch.Editors;

namespace SPAdvancedSearch
{
    /// <summary>
    /// Defines an advanced search property.
    /// </summary>
    [Serializable()]
    public class SearchProperty
    {
        #region Fields

        private string _managedPropertyName;
        private string _friendlyName;
        private string _settingsXml;
        private SearchPropertyValueType _searchValueType;
        private ValueControlBase _valueControl;
        private EditorControlBase _editorControl;

        #endregion

        #region Ctor

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the managed property name for the <see cref="SearchProperty"/>.
        /// </summary>
        [XmlAttribute()]
        public string ManagedPropertyName
        {
            get { return _managedPropertyName; }
            set { _managedPropertyName = value; }
        }

        /// <summary>
        /// Gets or sets the friendly name of the <see cref="SearchProperty"/>.
        /// </summary>
        [XmlAttribute()]
        public string FriendlyName
        {
            get { return _friendlyName;  }
            set { _friendlyName = value;  }
        }

        /// <summary>
        /// Gets or sets the type of property value for the <see cref="SearchProperty"/>.
        /// </summary>
        [XmlAttribute()]
        public SearchPropertyValueType SearchValueType
        {
            get { return _searchValueType; }
            set { _searchValueType = value; }
        }

        /// <summary>
        /// Gets or sets the serialized settings for the specific value control.
        /// </summary>
        [XmlElement()]
        public string SettingsXml
        {
            get 
            {
                if (null != this.GetEditorControl())
                {
                    return this.GetEditorControl().SettingsXml;
                }
                return string.Empty; 
            }
            set 
            { 
                _settingsXml = value; 
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the value control for the <see cref="SearchProperty"/>.
        /// </summary>
        public ValueControlBase GetValueControl()
        {
            if (_valueControl == null)
            {
                _valueControl = this.DetermineValueControlType();
                _editorControl = _valueControl.GetEditorControl();
                if (null != _editorControl)
                {
                    _editorControl.SettingsXml = _settingsXml;
                }
            }

            return _valueControl;
        }

        /// <summary>
        /// Gets the <see cref="EditorControlBase"/> used to manage the configuration settings for the current <see cref="SearchProperty"/>.
        /// </summary>
        /// <returns></returns>
        public EditorControlBase GetEditorControl()
        {
            if (_editorControl == null)
            {
                _editorControl = this.GetValueControl().GetEditorControl();
            }

            return _editorControl;
        }

        /// <summary>
        /// Creates an instance of the appropriate value control type.
        /// </summary>
        /// <returns></returns>
        private ValueControlBase DetermineValueControlType()
        {
            ValueControlBase valueControl = null;

            switch (this.SearchValueType)
            {
                case SearchPropertyValueType.TextBox:
                    valueControl = new TextBoxValueControl(this.ManagedPropertyName);
                    break;

                case SearchPropertyValueType.Choice:
                    valueControl = new ChoiceValueControl(this.ManagedPropertyName);
                    break;

                //case SearchPropertyValueType.Lookup:
                //    throw new NotSupportedException("Lookup value types are not supported in this release.");
            }

            return valueControl;
        }

        #endregion

    }
}
